﻿using System;
using System.Collections.Generic;
using System.Linq;
using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
using static CoreShop.Library.GlobalEnumVars;

namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsprintschemeServices : Repository<corecmsprintscheme>, IcorecmsprintschemeServices
    {
        public bool InsertAsync(corecmsprintscheme entity)
        {
            return base.Change<corecmsprintscheme>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsprintscheme> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsprintscheme entity)
        {
            return base.Change<corecmsprintscheme>().Update(a => new corecmsprintscheme()
            {
                gearName = entity.gearName,
                region = entity.region,
                printerName = entity.printerName,
                printerType = entity.printerType,
                printerIP = entity.printerIP,
                brandId = entity.brandId,
                paperType = entity.paperType,
                sendPrintWay = entity.sendPrintWay,
                buzzingCount = entity.buzzingCount,
                buzzingType = entity.buzzingType,
                emptyRows = entity.emptyRows,
                retryCount = entity.retryCount,
                printConnect = entity.printConnect,
                printPort = entity.printPort,
                baudrate = entity.baudrate,
                remark = entity.remark,
                spareSchemeId = entity.spareSchemeId,
                status = entity.status,
                address = entity.address,
                linkPhone = entity.linkPhone,
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsprintscheme> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsprintscheme>().Update(a => new corecmsprintscheme()
                {
                    gearName = item.gearName,
                    region = item.region,
                    printerName = item.printerName,
                    printerType = item.printerType,
                    printerIP = item.printerIP,
                    brandId = item.brandId,
                    paperType = item.paperType,
                    sendPrintWay = item.sendPrintWay,
                    buzzingCount = item.buzzingCount,
                    buzzingType = item.buzzingType,
                    emptyRows = item.emptyRows,
                    retryCount = item.retryCount,
                    printConnect = item.printConnect,
                    printPort = item.printPort,
                    baudrate = item.baudrate,
                    remark = item.remark,
                    spareSchemeId = item.spareSchemeId
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsprintscheme>().DeleteById(id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsprintscheme>().DeleteById(id);
            }
            return returnKey;
        }

        #region 获取缓存的所有数据==========================================================
        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsprintscheme> GetCaChe()
        {
            return base.GetList();
        }
        #endregion


        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsprintscheme> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsprintscheme> custos = new List<corecmsprintscheme>();
            var where = Expressionable.Create<corecmsprintscheme>();
            if (!string.IsNullOrEmpty(sName))
            {
                where = where.And(a => a.gearName.Contains(sName));
            }
            if (pageIndex >= 1 && pageSize > 0)
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count)
                    .OrderBy(a => a.id).ToList();
            }
            else
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            }
            return custos;
        }
        #endregion

        #region
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsprintscheme> QueryAsync(long type)
        {
            List<corecmsprintscheme> list = new List<corecmsprintscheme>();
            var where = Expressionable.Create<corecmsprintscheme>();
            if (type > 0)
                where = where.And(a => a.sendPrintWay == type);

            list = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            return list;
        }
        #endregion

        #region
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsprintscheme> GetKitchenPrinter()
        {
            List<corecmsprintscheme> list = new List<corecmsprintscheme>();
            var where = Expressionable.Create<corecmsprintscheme>();
            where = where.And(a => a.printerType == (int)GlobalEnumVars.printerType.Kitchen);
            //where = where.And(a => a.sendPrintWay == (int)GlobalEnumVars.SendPrintWay.Command);
            where = where.And(a => a.status == (int)GlobalEnumVars.PrinterStatus.Default);
            list = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            return list;
        }
        #endregion

        #region
        public List<corecmsprintscheme> SelectByName(string printerName, int printerType, int brandId, int sendPrintWay)
        {
            List<corecmsprintscheme> list = new List<corecmsprintscheme>();
            var where = Expressionable.Create<corecmsprintscheme>();
            if (printerName != null && printerName != "")
            {
                where = where.And(a => a.printerName.Contains(printerName));
            }
            if (printerType > 0)
            {
                where = where.And(a => a.printerType == printerType);
            }
            if (brandId > 0)
            {
                where = where.And(a => a.brandId == brandId);
            }
            if (sendPrintWay > 0)
            {
                where = where.And(a => a.sendPrintWay == sendPrintWay);
            }
            list = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            return list;
        }
        #endregion
                

        /// <summary>
        /// 根据打印任务，得到所有方案(即打印机)
        /// </summary>
        /// <param name="batchId"></param>
        /// <returns></returns>
        public corecmsprintscheme QueryPrintSchemeInfo(long printSchemeId, long billTypeId)
        {
            var printscheme = new corecmsprintschemeServices().GetById(printSchemeId);

            var printschemedetail = new corecmsprintschemedetailServices().AsQueryable()
                .Where(p=>p.printSchemeId == printSchemeId && p.billtypeId == billTypeId 
                && p.selectedFlag == true).First();

            if (printschemedetail != null)
            {               
                var billtemplates = new corecmsprintbilltemplateServices().AsQueryable()
                .Where(p => p.id == printschemedetail.templateId).ToList();
                //2024-04-24
                //printscheme.billtemplates = billtemplates;
                //if ((billtemplates != null) && (billtemplates.Count > 0))
                //{
                //    var billtemplate = billtemplates.First();
                //    printscheme.foodPrintType = billtemplate.foodPrintType;
                //}                
            }

            return printscheme;

            //var list = new corecmsprintschemedetailServices().AsQueryable()
            //    .InnerJoin<corecmsprintbilltemplate>((a, b) => a.templateId == b.id)
            //    .Where((a, b) => a.printSchemeId == printSchemeId && a.billtypeId == billTypeId && a.selectedFlag == true)
            //    .Select((a, b) => new corecmsprintscheme
            //    {
            //        id = a.id,
            //        foodPrintType = b.foodPrintType
            //    }).With(SqlWith.NoLock).ToList();

            //var printscheme = new corecmsprintschemeServices().GetById(printSchemeId);

            //if ((printscheme != null) && (list != null) &&(list.Count > 0))
            //{
            //    var item = list.First();
            //    printscheme.foodPrintType = item.foodPrintType;
            //    printscheme.printerName = item.printerName;                
            //}

            //return printscheme;
        }

        /// <summary>
        /// 根据id查询单条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public corecmsprintscheme SelectById(int id)
        {
            return base.Change<corecmsprintscheme>().GetSingle(a => a.id == id);
        }
    }
}